package ejb.contentManagement;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ejb.dto.LocationDTO;
import entities.Location;
import entities.Nation;


@Stateless
public class LocationBean implements LocationMgr {
	
	@PersistenceContext
    private EntityManager em;
	
	/**
	 * Retrieve entity Location from the associated Id
	 * @param id
	 * @return entity Location associated to the <code>id</code>
	 */
	public Location getLocation(int id) {
		return em.find(Location.class, id);
	}
	
	/**
	 * Retrieve all Location entities
	 * @return all Location entities
	 */
	public List<Location> getAllLocations() {
    	return em.createNamedQuery("Location.findAll", Location.class)
                .getResultList();
    }
	
	/**
	 * Retrieve entity Location from the associated Id and convert it to corresponding LocationDTO
	 */
	@Override
	public LocationDTO getLocationDTO(int id) {
		return  convertToDTO(getLocation(id));
    }
	
	/**
	 * Retrieve all Location entities and convert them into corresponding list of LocationDTO
	 */
	@Override
	public List<LocationDTO> getAllLocationsDTO() {
		List<Location> list = getAllLocations();
		List<LocationDTO> newList = new ArrayList<LocationDTO>();
		for(Location obj : list) {
			newList.add(convertToDTO(obj));
		}
		return newList;
	}
	
	/**
	 * Convert entity Location (parameter <code>l</code>) into corresponding LocationDTO 
	 * @param l
	 * @return converted LocationDTO
	 */
	public static LocationDTO convertToDTO(Location l) {
		if(l == null){
			return null;
		}
		LocationDTO dto = new LocationDTO();
		dto.setId(l.getId());
		dto.setName(l.getName());
		dto.setDescription(l.getDescription());
		dto.setNation(NationBean.convertToDTO(l.getNation()));
		return dto;
	}
	
	/**
	 * Retrieve entity Location from the associated Id and delete the corresponding element from the database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean deleteLocation(int id) {
		try{
			em.remove(em.find(Location.class, id));
			em.flush();
		}
		//Catch exceptions in the delete operation from database
		catch(Exception e){
			return false;
		}
		return true;
	}
	
	/**
	 * Create entity Location from corresponding LocationDTO and update corresponding element in database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean updateLocation(LocationDTO updatedLocation) {
		if(updatedLocation.getName().equals("") || 
			updatedLocation.getName().startsWith(" ") || updatedLocation.getName()==null)
			return false;
		try{
			em.merge(new Location(updatedLocation, em.find(Nation.class, updatedLocation.getNationId())));
			em.flush();
		}
		//Catch exceptions in the update operation of database
		catch(Exception e){
			return false;
		}
		return true;
	}
	
	/**
	 * Create entity Location from corresponding LocationDTO and insert new corresponding element in database
	 */
	@Override
	@RolesAllowed({"ADMINS"})
	public boolean insertLocation(LocationDTO newLocation) {
		if(newLocation.getName().equals("") || newLocation.getName().startsWith(" ") || newLocation.getName()==null)
			return false;
		try{
			em.persist(new Location(newLocation, em.find(Nation.class, newLocation.getNationId())));
			em.flush();
		}
		//Catch exceptions in the insert operation in database
		catch(Exception e){
			return false;
		}
		return true;
	}

}
